#include "RingQueue.hpp"
#include "Task.hpp"
#include <pthread.h>
#include <ctime>
#include <cstdlib>
#include <sys/types.h>
#include <unistd.h>
const string oper = "+-*/%";

void *ProductRoutine(void *rq)
{
    RingQueue<Task> *rq_ = static_cast<RingQueue<Task> *>(rq);
    while(true)
    {
        sleep(1);
        int x=rand()%10+10;
        int y=rand()%5+10;
        char op = oper[rand()%oper.size()];
        Task t(x,y,op,mymath);
        cout<<"生产任务完成："<<t.toTaskString()<<endl;
        rq_->Push(t);
    }
    return nullptr;
}

void *ConsumeRoutine(void *rq)
{
    RingQueue<Task> *rq_ = static_cast<RingQueue<Task> *>(rq);
    while(true)
    {
        // sleep(1);
        Task t;
        rq_->Pop(&t);
        string result = t();
        cout<<"消费任务完成："<<result<<endl;
    }
    return nullptr;
}
int main()
{
    srand((unsigned int)time(nullptr)^0x2836^getpid()^pthread_self());
    
    pthread_t p, c;
    RingQueue<Task> *rq = new RingQueue<Task>();

    pthread_create(&p, nullptr, ProductRoutine, rq);
    pthread_create(&c, nullptr, ConsumeRoutine, rq);

    pthread_join(p,nullptr);
    pthread_join(c,nullptr);
}